package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class ChatroomPeer extends BasePeer{
	public static final String tableName = "Chatroom";
	public static final String CHATROOMID = "chatroomId";
	public static final String TITLE = "title";
	public static final String LASTACTIVE = "lastActive";
	public static final String[] columnNames=new String[]{"chatroomId","title","lastActive"};
	public static Chatroom getChatroomByPKs(Object chatroomId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ChatroomPeer.tableName,ChatroomPeer.columnNames," TRUE  AND `chatroomId`=? ",new Object[]{chatroomId,},0, 1,conn);
			Chatroom chatroom = new Chatroom();
		fill(rs,chatroom);
		return chatroom;
	}
	public static Chatroom getChatroomReferenceColumnsOnlyByPKs(Object chatroomId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ChatroomPeer.tableName,new String[]{"chatroomId",}," TRUE AND `chatroomId`=?",new Object[]{chatroomId,}, 0,  1,conn);
			Chatroom chatroom = new Chatroom();
		fill(rs,chatroom);
		return chatroom;
	}
	public static Chatroom getChatroomByChatroomId(long chatroomId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ChatroomPeer.tableName,ChatroomPeer.columnNames,"`chatroomId`=?",new Object[]{chatroomId},0, 1,conn);
			Chatroom chatroom = new Chatroom();
		fill(rs,chatroom);
		return chatroom;
	}
	public static void fill(ResultSet rs, Chatroom chatroom) throws SQLException {
		if(rs.next()){
			try{chatroom.setChatroomId(rs.getLong("chatroomId"));}catch(Exception ex){}
			try{chatroom.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{chatroom.setLastActive(rs.getTimestamp("lastActive"));}catch(Exception ex){}
			chatroom.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Chatroom> chatroomCollection) throws SQLException {
		while(rs.next()){
			Chatroom chatroom=new Chatroom();
			try{chatroom.setChatroomId(rs.getLong("chatroomId"));}catch(Exception ex){}
			try{chatroom.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{chatroom.setLastActive(rs.getTimestamp("lastActive"));}catch(Exception ex){}
			chatroom.resetModified();
			chatroomCollection.add(chatroom);
		}
	}
	public static boolean update(Chatroom chatroom, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < ChatroomPeer.columnNames.length; i++) {
			if (((ChatroomBase)chatroom).modified[i]) {
				columns.add(ChatroomPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Chatroom` SET ");
		sql.append("WHERE TRUE AND `chatroomId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,chatroom.get(column));
		}
		pstmt.setObject(columns.size()+1+0,chatroom.get("chatroomId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Chatroom chatroom, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (chatroom.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Chatroom` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (chatroom.get(column) != null) {
				pstmt.setObject(i + 1 + j, chatroom.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Chatroom> getChatroomCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Chatroom> collection=new Vector<Chatroom>();
		fill(rs,collection);
		return collection;
	}
}
